import java.io.*;
import java.util.Scanner;

//文件内容的复制
public class Demo_CopyFile {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要复制的文件：");
        String rootPath = scanner.next();
        File file = new File(rootPath);
        if(!file.exists()){
            System.out.println("文件不存在，请确认文件路径是否正确");
            return;
        }
        if(!file.isFile()){
            System.out.println("不是普通文件，请确认路径是否正确");
            return;
        }
        System.out.println("请输入要复制到的目标文件：");
        String destPath = scanner.next();
        File destFile = new File(destPath);
        if(!destFile.exists()){
            System.out.println("目标文件不存在，请确认路径是否正确");
            return;
        }
        if(destFile.isDirectory()){
            System.out.println("目标文件是一个目录，请确认文件路径是否正确");
            return;
        }
        if(destFile.isFile()){
            System.out.println("目标文件已存在，请确认是否要进行覆盖(y/n)：");
            String choice = scanner.next();
            if(!choice.equals("Y") && !choice.equals("y")){
                System.out.println("停止复制");
                return;
            }
        }
        try(InputStream inputStream = new FileInputStream(rootPath);
            OutputStream outputStream = new FileOutputStream(destFile, true)){
            while(true){
                byte[] b = new byte[1024];
                int i = inputStream.read(b);
                if(i == -1){
                    break;
                }
                outputStream.write(b, 0, i);
                outputStream.flush();
            }
            System.out.println("复制已完成");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
